package Eyjafjalla.patch;

import Eyjafjalla.blight.DreamKey;
import Eyjafjalla.card.skill.DollyInvitation_Eyjafjalla;
import Eyjafjalla.character.Eyjafjalla;
import Eyjafjalla.dungeons.TheSiesta;
import Eyjafjalla.helper.ModConfig;
import Eyjafjalla.modcore.LibraryEnum;
import basemod.ReflectionHacks;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.evacipated.cardcrawl.modthespire.lib.*;
import com.megacrit.cardcrawl.audio.MainMusic;
import com.megacrit.cardcrawl.audio.TempMusic;
import com.megacrit.cardcrawl.blights.AbstractBlight;
import com.megacrit.cardcrawl.characters.AbstractPlayer;
import com.megacrit.cardcrawl.core.CardCrawlGame;
import com.megacrit.cardcrawl.core.Settings;
import com.megacrit.cardcrawl.dungeons.AbstractDungeon;
import com.megacrit.cardcrawl.helpers.BlightHelper;
import com.megacrit.cardcrawl.helpers.ImageMaster;
import com.megacrit.cardcrawl.helpers.TipHelper;
import com.megacrit.cardcrawl.helpers.controller.CInputActionSet;
import com.megacrit.cardcrawl.helpers.input.InputHelper;
import com.megacrit.cardcrawl.map.DungeonMap;
import com.megacrit.cardcrawl.map.MapEdge;
import com.megacrit.cardcrawl.map.MapRoomNode;
import com.megacrit.cardcrawl.relics.AbstractRelic;
import com.megacrit.cardcrawl.rewards.RewardItem;
import com.megacrit.cardcrawl.rooms.AbstractRoom;
import com.megacrit.cardcrawl.rooms.MonsterRoomBoss;
import com.megacrit.cardcrawl.rooms.TreasureRoomBoss;
import com.megacrit.cardcrawl.rooms.TrueVictoryRoom;
import com.megacrit.cardcrawl.saveAndContinue.SaveFile;
import com.megacrit.cardcrawl.screens.CombatRewardScreen;
import com.megacrit.cardcrawl.screens.DoorUnlockScreen;
import com.megacrit.cardcrawl.screens.DungeonMapScreen;
import com.megacrit.cardcrawl.ui.buttons.ProceedButton;
import com.megacrit.cardcrawl.ui.panels.TopPanel;

import java.util.Iterator;

public class SiestaPatch {
    public static String BOSS_KEY = "";

    public static boolean EnterSista = false;

    private static final float ICON_W = 64.0F * Settings.scale;
    public static final float ICON_Y;
    public static final String PinkKeyName;
    public static final String PinkKeyDescription;

    @SpirePatch(clz = CardCrawlGame.class,method = "getDungeon",paramtypez = {String.class, AbstractPlayer.class})
    public static class GetDungeonPatch{
        @SpirePostfixPatch
        public static AbstractDungeon Postfix(AbstractDungeon _ret, CardCrawlGame _inst, String key, AbstractPlayer p){

                if(key.equals(TheSiesta.ID))
                    return new TheSiesta(p,AbstractDungeon.specialOneTimeEventList);

            return _ret;
        }
    }

    @SpirePatch(clz = CardCrawlGame.class,method = "getDungeon",paramtypez = {String.class,AbstractPlayer.class, SaveFile.class})
    public static class GetDungeonOnSavePatch{
        @SpirePostfixPatch
        public static AbstractDungeon Postfix(AbstractDungeon _ret, CardCrawlGame _inst, String key, AbstractPlayer p,SaveFile file){

                if(key.equals(TheSiesta.ID))
                    return new TheSiesta(p,file);

            return _ret;
        }
    }

    @SpirePatch(
            clz = MainMusic.class,
            method = "getSong"
    )
    public static class MainMusicPatch{
        @SpirePostfixPatch
        public static Music Postfix(Music _result, MainMusic _inst, String key){
            if(key.equals(TheSiesta.ID)){
                return MainMusic.newMusic("EyjafjallaResources/audio/main.ogg");
            }
            else if(key.equals("eyjafjalla:Dolly")){
                return MainMusic.newMusic("EyjafjallaResources/audio/dolly.ogg");
            }

            return _result;
        }
    }

    @SpirePatch(
            clz = TempMusic.class,
            method = "getSong"
    )
    public static class TempMusicPatch{
        @SpirePostfixPatch
        public static Music Postfix(Music _result, TempMusic _inst,String key){
            if(key.equals("BOSS_BEYOND")){
                if(BOSS_KEY.equals("DOLLY")){
                    return MainMusic.newMusic("EyjafjallaResources/audio/dolly.ogg");
                }
            }
            return _result;
        }
    }


    @SpirePatch(clz = DungeonMap.class,method = "calculateMapSize")
    public static class MapSizePatch{
        @SpirePostfixPatch
        public static float Postfix(float _ret,DungeonMap _inst){
            if(AbstractDungeon.id.equals(TheSiesta.ID))
                return Settings.MAP_DST_Y * 4F - 960F*Settings.scale;
            return _ret;
        }
    }

    @SpirePatch(clz = DungeonMap.class,method = "update")
    public static class UpdatePatch {
        @SpireInsertPatch(rloc = 40)
        public static void Insert(DungeonMap _inst) {
            if (_inst.bossHb.hovered && (InputHelper.justClickedLeft || CInputActionSet.select.isJustPressed())) {
                if (AbstractDungeon.getCurrRoom().phase == AbstractRoom.RoomPhase.COMPLETE && AbstractDungeon.screen == AbstractDungeon.CurrentScreen.MAP) {
                    if ((Settings.isDebug || AbstractDungeon.id.equals(TheSiesta.ID) && AbstractDungeon.getCurrMapNode().y == 5)) {
                        AbstractDungeon.getCurrMapNode().taken = true;
                        MapRoomNode node2 = AbstractDungeon.getCurrMapNode();
                        Iterator var2 = node2.getEdges().iterator();

                        while (var2.hasNext()) {
                            MapEdge e = (MapEdge) var2.next();
                            if (e != null) {
                                e.markAsTaken();
                            }
                        }

                        InputHelper.justClickedLeft = false;
                        CardCrawlGame.music.fadeOutTempBGM();
                        MapRoomNode node = new MapRoomNode(-1, 15);
                        node.room = new MonsterRoomBoss();
                        AbstractDungeon.nextRoom = node;
                        if (AbstractDungeon.pathY.size() > 1) {
                            AbstractDungeon.pathX.add(AbstractDungeon.pathX.get(AbstractDungeon.pathX.size() - 1));
                            AbstractDungeon.pathY.add((Integer) AbstractDungeon.pathY.get(AbstractDungeon.pathY.size() - 1) + 1);
                        } else {
                            AbstractDungeon.pathX.add(1);
                            AbstractDungeon.pathY.add(15);
                        }

                        AbstractDungeon.nextRoomTransitionStart();
                        _inst.bossHb.hovered = false;
                    }
                }
            }
        }
    }

    @SpirePatch(clz = DungeonMap.class,method = "render")
    public static class RenderPatch{
        @SpirePrefixPatch
        public static SpireReturn<Void> Prefix(DungeonMap _inst, SpriteBatch sb){
            if(AbstractDungeon.id.equals(TheSiesta.ID)){
                ReflectionHacks.privateMethod(DungeonMap.class,"renderFinalActMap", SpriteBatch.class).invoke(_inst, sb);
                return SpireReturn.Return();
            }
            return SpireReturn.Continue();
        }
    }

    @SpirePatch(clz = DungeonMap.class,method = "renderMapBlender")
    public static class RenderBlenderPatch{
        @SpirePrefixPatch
        public static SpireReturn<Void> Prefix(DungeonMap _inst, SpriteBatch sb){
            if(AbstractDungeon.id.equals(TheSiesta.ID))
                return SpireReturn.Return();
            return SpireReturn.Continue();
        }
    }

    @SpirePatch(clz = DungeonMapScreen.class,method = "open")
    public static class OpenMapPatch{
        @SpireInsertPatch(rloc = 8)
        public static void Insert(DungeonMapScreen _inst,boolean doScrollingAnimation){
            if(AbstractDungeon.id.equals(TheSiesta.ID)){
                ReflectionHacks.setPrivate(_inst,DungeonMapScreen.class,"mapScrollUpperLimit",-600.0F * Settings.scale);
            }
        }
    }

    @SpirePatch(clz = ProceedButton.class,method = "update")
    public static class EndSiestaPatch{
        @SpireInsertPatch(rloc = 40)
        public static void Insert(ProceedButton _inst){
            if (AbstractDungeon.getCurrRoom() instanceof MonsterRoomBoss) {
                if (AbstractDungeon.id.equals(TheSiesta.ID)) {
                    CardCrawlGame.music.fadeOutBGM();
                    MapRoomNode node = new MapRoomNode(3, 7);
                    node.room = new TrueVictoryRoom();
                    AbstractDungeon.nextRoom = node;
                    AbstractDungeon.closeCurrentScreen();
                    AbstractDungeon.nextRoomTransitionStart();
                    _inst.hide();
                }
            }
        }
    }

    @SpirePatch(clz = BlightHelper.class,method = "getBlight")
    public static class GetBlightPatch{
        @SpirePostfixPatch
        public static AbstractBlight Postfix(AbstractBlight _ret,String id){
            if(id.equals(DreamKey.ID))
                return new DreamKey();
            return _ret;
        }
    }

    //get key
    @SpirePatch(clz = CombatRewardScreen.class,method = "setupItemReward")
    public static class RewardPatch{
        @SpirePostfixPatch
        public static void Postfix(CombatRewardScreen _inst){
            if(!(AbstractDungeon.player instanceof Eyjafjalla)&&!ModConfig.enable)
                return;
            if(!Settings.isFinalActAvailable||!(AbstractDungeon.getCurrRoom() instanceof MonsterRoomBoss))
                return;
            if(AbstractDungeon.player.hasBlight(DreamKey.ID)||AbstractDungeon.player.masterDeck.findCardById(DollyInvitation_Eyjafjalla.ID)!=null)
                return;
            RewardItem item = new RewardItem();
            item.cards.clear();
            item.cards.add(new DollyInvitation_Eyjafjalla());
            _inst.rewards.add(item);
            _inst.positionRewards();
        }
    }

    //change true victory
    @SpirePatch(clz = TrueVictoryRoom.class,method = "onPlayerEntry")
    public static class TrueVictoryPatch{
        @SpirePrefixPatch
        public static SpireReturn<Void> Prefix(TrueVictoryRoom _inst){
            if(AbstractDungeon.player.hasBlight(DreamKey.ID)&&!EnterSista){
                CardCrawlGame.stopClock = false;
                EnterSista = true;
                CardCrawlGame.music.fadeOutTempBGM();
                MapRoomNode node = new MapRoomNode(-1, 15);
                node.room = new TreasureRoomBoss();
                AbstractDungeon.nextRoom = node;
                AbstractDungeon.closeCurrentScreen();
                CardCrawlGame.dungeon.nextRoomTransition();
                return SpireReturn.Return();
            }
            return SpireReturn.Continue();
        }
    }

    //dungeon
    @SpirePatch(clz = TreasureRoomBoss.class,method = "getNextDungeonName")
    public static class TreasureBossPatch{
        @SpirePostfixPatch
        public static String Postfix(String _ret,TreasureRoomBoss _inst){
            if(EnterSista&&!AbstractDungeon.id.equals(TheSiesta.ID)){
                return TheSiesta.ID;
            }
            return _ret;
        }
    }

    //dungeon actlikeit
    @SpirePatch(clz = ProceedButton.class,method = "goToNextDungeon")
    public static class ProceedButtonTPatch{
        @SpirePostfixPatch
        public static void Postfix(ProceedButton _inst){
            if(EnterSista&&!AbstractDungeon.id.equals(TheSiesta.ID)){
                CardCrawlGame.nextDungeon = TheSiesta.ID;
            }
        }
    }



    static {
        String[] ss = CardCrawlGame.languagePack.getUIString(TheSiesta.ID).TEXT;
        PinkKeyName = ss[1];
        PinkKeyDescription = ss[2];
        ICON_Y = Settings.isMobile ? (float)Settings.HEIGHT - ICON_W - 12.0F * Settings.scale : (float)Settings.HEIGHT - ICON_W;
    }
}
